package
{
	import Button.UglyButton;
	import Button.UglyTextField;
	
	import Const.ButtonIDs;
	
	import Screen.*;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.system.*;
	import flash.text.*;
	
	[SWF (width = "800", height = "600", frameRate = "30")]
	public class TpFisica extends Sprite
	{		
		private var _buttonsBar : Sprite = new Sprite();
		private var _screenSpace : Sprite = new Sprite();
		
		//Buttons
		private var _trainsButton : UglyButton = new UglyButton("Trenes", ButtonIDs.BUTTONID_TRAIN);
		private var _stagesButton : UglyButton = new UglyButton("Estaciones", ButtonIDs.BUTTONID_SECTOR);
		private var _resultsButton : UglyButton = new UglyButton("Resultados", ButtonIDs.BUTTONID_RESULT);
		
		//Const
		static private var BUTTON_BAR_HEIGHT : int = 40;
		static private var BUTTON_POS_X : int= 22;
		static private var BUTTON_POS_Y : int = 10;
		
		//Screens
		private var _actualScreen : GenericScreen;
		private var _trainScreen : TrainScreen;
		private var _stationScreen : StationScreen;
		private var _resultScreen : ResultScreen;
		
		public function TpFisica()
		{			
			//Buttons Bar
			_buttonsBar.x = 0;
			_buttonsBar.y = 0;
			
			with(_buttonsBar.graphics)
			{			
				beginFill(0xA4A4A4, 1);
				drawRect(0, 0, flash.system.Capabilities.screenResolutionX, BUTTON_BAR_HEIGHT);
				endFill();
			}
			
			//Screen Space
			_screenSpace.x = 0;
			_screenSpace.y = BUTTON_BAR_HEIGHT;
			
			with(_screenSpace.graphics)
			{
				beginFill(0xEFEFFB, 1);
				drawRect(0, 0, flash.system.Capabilities.screenResolutionX, flash.system.Capabilities.screenResolutionY - BUTTON_BAR_HEIGHT);
				endFill();
			}
			
			//Set buttons position
			_trainsButton.x = BUTTON_POS_X;
			_trainsButton.y = BUTTON_POS_Y;
			
			_stagesButton.x = BUTTON_POS_X * 15;
			_stagesButton.y = BUTTON_POS_Y;
			
			_resultsButton.x = BUTTON_POS_X * 30; 
			_resultsButton.y = BUTTON_POS_Y;
			
			//Adding buttons to the bar
			_buttonsBar.addChild(_trainsButton);
			_buttonsBar.addChild(_stagesButton);
			_buttonsBar.addChild(_resultsButton);
			
			//Add screen space to stage
			addChild(_screenSpace);
			//Add the button bar to the stage
			addChild(_buttonsBar);
			
			//Add all the listeners
			addAllEventListener();
			
			generateAllScreen();
		}		
		
		private function addAllEventListener() : void
		{
			_trainsButton.addEventListener(MouseEvent.CLICK, changeScreen);
			_stagesButton.addEventListener(MouseEvent.CLICK, changeScreen);
			_resultsButton.addEventListener(MouseEvent.CLICK, changeScreen);
		}
		
		private function generateAllScreen() : void
		{
			_trainScreen = new TrainScreen();
			_stationScreen = new StationScreen();
			_resultScreen = new ResultScreen();
			
			_actualScreen = _trainScreen;
			
			_screenSpace.addChild(_actualScreen);
		}
		
		private function changeScreen(evt:MouseEvent) : void
		{			
			switch(evt.target.valueOf().getId())
			{
				case ButtonIDs.BUTTONID_TRAIN:
					if(_actualScreen != _trainScreen)
					{
						_screenSpace.removeChild(_actualScreen);
						_screenSpace.addChild(_trainScreen);
						_actualScreen = _trainScreen;
					}
					break;
				case ButtonIDs.BUTTONID_SECTOR:
					if(_actualScreen != _stationScreen)
					{
						_screenSpace.removeChild(_actualScreen);
						_screenSpace.addChild(_stationScreen);
						_actualScreen = _stationScreen;
					}
					break;
				case ButtonIDs.BUTTONID_RESULT:
					if(_actualScreen != _resultScreen)
					{
						_screenSpace.removeChild(_actualScreen);
						_screenSpace.addChild(_resultScreen);
						_actualScreen = _resultScreen;
					}
					break;
			}
		}
	}
}


//LO ANTERIOR
/*package
{
	import Ecuations.MinTime;
	
	import Screen.GraphicScreen;
	
	import Train.BaseTrain;
	import Train.PassengerTrain;
	import Train.TrainFactory;
	
	import flash.display.Sprite;
	
	[SWF (width = "800", height = "600", frameRate = "31")]
	public class TpFisica extends Sprite
	{
		public function TpFisica()
		{
			var distance:Number = 250;
			
			//crea 3 estaciones separadas por 100 cada una
			DistanceObjCreator.createDistObj(0, 100, 0, 10 );
			DistanceObjCreator.createDistObj(100, 500, 0, 50 );
			//DistanceObjCreator.createDistObj( 500, true, 0, 100 );
			//DistanceObjCreator.createDistObj( 920, true, 0, 100 );
			
			//testeando q se creen bien las estaciones, esto desp se crea segun 
			//lo que se setee a travez de la interface
			var stationList:Array = DistanceObjCreator.stationsList;
			for (var i:int = 0; i < stationList.length; i++)
			{
				trace( "Station " + i + " Init Pos : " + stationList[i].initPos );
				trace( "Station " + i + " Dest Pos : " + stationList[i].destPos );
				trace( "Station " + i + " MaxVelocity : " + stationList[i].segmentVMax );
				
				trace("=========================================================");
			}
			
			
			var train:PassengerTrain = TrainFactory.createTrain( BaseTrain.PASSENGER_TRAIN ) as PassengerTrain;
			train.accel = 1.5;
			train.brakeAccel = -0.75;
			train.deriveAccel = -0.65;
			train.trainVMax = 135;
			
			//para probar nomas
			var totalObjList:Array = new Array();
			
			for (var j:int = 0; j < stationList.length; j++)
			{
				var useEcc:MinTime = new MinTime( train, stationList[j], this.stage );
				var station:DistanceObject = stationList[j];
				station.trainStops = true; //HARDCODEADO
				
				//if (!station.trainStops)
				//{
				//	station.trainInitVelocity = stationList[j - 1] ? stationList[j - 1].trainFinalVelocity : 0;
				//	station.trainFinalVelocity = stationList[j + 1] ? stationList[j + 1].segmentVMax : 0;
					
				//	useEcc.calculate(MinTime.USE_MIN_TIME, station.trainInitVelocity, station.trainFinalVelocity);	
				//}
				//else
				//{
					useEcc.calculate(MinTime.USE_MAX_DERIVE, 0, 0);
				//}
				
					for(var k:int = 0; k < useEcc.dataObjectList.length; k++)
					{
						totalObjList.push( useEcc.dataObjectList[k] );
					}
					
				}
			
			var graphicScreenTest:GraphicScreen = new GraphicScreen(stationList, totalObjList);
			graphicScreenTest.x = 20;
			stage.addChild( graphicScreenTest );
			
			graphicScreenTest.velocidadEnFuncionDelLaProgresiva();
			//graphicScreenTest.velocidadEnFuncionDelTiempo();
			
			//graphicScreenTest.aceleracionEnFuncionDelTiempo();
			//graphicScreenTest.posicionEnFuncionDelTiempo();			
		}
	}
}*/